package com.jsoft.controller;

import com.jsoft.annotation.Log;
import com.jsoft.entity.*;
import com.jsoft.service.JobService;
import com.jsoft.util.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * (Job)表控制层
 *
 * @author makejava
 * @since 2025-03-14 23:30:25
 */
@RestController
@RequestMapping("job")
public class JobController {
    /**
     * 服务对象
     */
    @Resource
    private JobService jobService;
    private PageBean pageBean = new PageBean();

    /**
     * 分页查询我的职位
     * @param jobName 职位名
     * @param userId 用户id
     * @param page 第几页
     * @param size 一页多少条
     * @return 查询结果
     */
    @GetMapping("getMyJobs")
    public Result<PageBean<JobAndEnterpriseAndIndustry>> getMyJobs(String jobName,String userId, int page, int size) {
        try {
            List<JobAndEnterpriseAndIndustry> jobs = jobService.getMyJobs(jobName,userId, page, size);
            long i = jobService.countMyJobs(jobName,userId);
            PageBean<JobAndEnterpriseAndIndustry> pageBean = new PageBean();
            pageBean.setTotal(i);
            pageBean.setItems(jobs);
            return Result.success(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("777系统繁忙，请稍后再试");
        }
    }

    /**
     * 三表联查，附带总条数
     *
     * @param jobName 职位名
     * @param page 第几页
     * @param size 一页多少条
     * @return
     */
    @GetMapping("jobs")
    public Result<PageBean<JobAndEnterpriseAndIndustry>> selectJobAndEnterpriseAndIndustry(String jobName, int page, int size) {
        try {
        List<JobAndEnterpriseAndIndustry> jobs = jobService.selectJobAndEnterpriseAndIndustry(jobName, page, size);
        long i = jobService.countJobs(jobName);
        PageBean<JobAndEnterpriseAndIndustry> pageBean = new PageBean();
        pageBean.setTotal(i);
        pageBean.setItems(jobs);
        return Result.success(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("666系统繁忙，请稍后再试");
        }
    }

    /**
     * 获取下拉菜单要显示的职位名
     * @return 查询结果
     */
    @GetMapping("/getJobNames")
    public Result<List<Job>> getJobNames() {
        List<Job> jobNames = jobService.getJobNames();
        return Result.success(jobNames);
    }

    /**
     * 通过企业id查询该企业的职位
     * @param enterpriseId 企业id
     * @return 查询结果
     */
    @GetMapping("/getJobsByEnterpriseId")
    public Result<List<Job>> getJobsByEnterpriseId(Integer enterpriseId) {
        List<Job> jobsByEnterpriseId = jobService.getJobsByEnterpriseId(enterpriseId);
        return Result.success(jobsByEnterpriseId);
    }

    /**
     * 分页查询
     *
     * @param job         筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Job>> queryByPage(Job job, PageRequest pageRequest) {
        return ResponseEntity.ok(this.jobService.queryByPage(job, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
   /* @GetMapping("{id}")
    public ResponseEntity<Job> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.jobService.queryById(id));
    }*/

    /**
     * 新增数据
     *
     * @param job 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Job> add(Job job) {
        return ResponseEntity.ok(this.jobService.insert(job));
    }

    public JobController() {

    }
    /**
     * 编辑数据
     *
     * @param job 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Job> edit(Job job) {
        return ResponseEntity.ok(this.jobService.update(job));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.jobService.deleteById(id));
    }


    /**
     * 查询高薪工作
     *
     * @return
     */
    @GetMapping("/high")
    public Result<PageBean<Job>> list(@RequestParam @NotNull Integer pageNum,
                                      @RequestParam @NotNull Integer pageSize) {
        PageBean<Job> pb = jobService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    /**
     * 根据工作中企业的id查询企业
     * @return
     */
    @GetMapping("/enterprise")
    public Result<PageBean<Enterprise>> queryenterprise(@RequestParam @NotNull Integer pageNum,
                                                        @RequestParam @NotNull Integer pageSize,
                                                        @NotEmpty Integer id){
    PageBean<Enterprise> pb = jobService.queryId(pageNum,pageSize,id);
    return Result.success(pb);
    }

    @GetMapping("/queryById")
    public Result<PageBean<Job>> queryJobById(@RequestParam @NotNull Integer pageNum,
                                              @RequestParam @NotNull Integer pageSize,
                                              @NotEmpty Integer id){

    PageBean<Job> pb = jobService.queryJobById(pageNum,pageSize,id);
    return Result.success(pb);


    }

    /**
     * 根据行业id查询职业
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/jobByIndustryId")
    public Result<PageBean<Job>> jobByIndustryId(@RequestParam @NotNull Integer pageNum,
                                                 @RequestParam @NotNull Integer pageSize,
                                                 @NotEmpty Integer id){

        PageBean<Job> pb = jobService.jobByIndustryId(pageNum,pageSize,id);
        return Result.success(pb);

    }


}

